package pers.hl.module_main.admin

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import pers.hl.common.base.BaseVBVMActivity
import pers.hl.lib_global.arouter.RouterPath
import pers.hl.lib_global.common.MMKVHelper
import pers.hl.lib_global.common.RouteUtils
import pers.hl.module_main.R
import pers.hl.module_main.admin.book.BookManageActivity
import pers.hl.module_main.admin.booktag.TagManageActivity
import pers.hl.module_main.admin.lib.LibManageActivity
import pers.hl.module_main.admin.user.UserManageActivity
import pers.hl.module_main.common.BaseLibQuickAdapter
import pers.hl.module_main.databinding.ActivityAdminBinding
import java.util.*

@Route(path = RouterPath.mainAdmin)
class AdminActivity : BaseVBVMActivity<ActivityAdminBinding, AdminViewModel>() {

    private lateinit var adapter: BaseQuickAdapter<AdminMenu, BaseViewHolder>

    override fun initView(savedInstanceState: Bundle?) {
        initTitle()
        initAdapter()
    }

    private fun initTitle() {
        mBinding.include.apply {
            toolbar.title = "管理员{${MMKVHelper.getUser()?.userName}}，欢迎您"
            ivBarRight.setImageResource(R.drawable.ic_close)
            ivBarRight.setOnClickListener {
                MMKVHelper.logout()
                RouteUtils.navToLogin()
                finish()
            }
        }
    }

    private fun initAdapter() {
        adapter = object : BaseLibQuickAdapter<AdminMenu, BaseViewHolder>(R.layout.item_rv_admin) {
            override fun convert(holder: BaseViewHolder, item: AdminMenu) {
                holder.getView<TextView>(R.id.tvMenuTitle).text = item.name
                holder.itemView.setOnClickListener {
                    var clazz: Class<out Activity>? = null
                    when (item.id) {
                        AdminMenu.OP_USER -> clazz = UserManageActivity::class.java

                        AdminMenu.OP_LIB -> clazz = LibManageActivity::class.java

                        AdminMenu.OP_BOOK -> clazz = BookManageActivity::class.java

                        AdminMenu.OP_TAG -> clazz = TagManageActivity::class.java

                        AdminMenu.OP_EXIT -> {
                            RouteUtils.navToLogin()
                            finish()
                        }
                    }
                    clazz?.let {
                        val intent = Intent(context, it)
                        context.startActivity(intent)
                    }
                }
            }

        }
        mBinding.apply {
            rvAdmin.layoutManager = LinearLayoutManager(this@AdminActivity)
            rvAdmin.adapter = adapter
        }
    }

    override fun initData(savedInstanceState: Bundle?) {
        val list = mutableListOf(
            AdminMenu(AdminMenu.OP_USER, "用户管理"),
            AdminMenu(AdminMenu.OP_LIB, "图书馆管理"),
            AdminMenu(AdminMenu.OP_BOOK, "书籍管理"),
            AdminMenu(AdminMenu.OP_TAG, "标签管理"),
            AdminMenu(AdminMenu.OP_EXIT, "退出登录"))
        adapter.setList(list)
    }
}